<template>
  <div id="puzzle" ref="puzzle" style="display:inline-block;">
  <!-- :style="'padding:' + 16*scale + 'px ' + 16*scale + 'px ' + 28*scale + 'px;border-radius:'+16*scale+'px;'" -->
    <div class="puzzle-container"
    :style="move ? 'visibility:hidden' : ''"
     >
      <div :style="'position:relative;overflow:hidden;width:'+ dataWidth +'px;'">
        <div :style="'position:relative;width:' + dataWidth + 'px;height:' + dataHeight + 'px;'">
          <img
            id="scream"
            ref="scream"
            src="../assets/images/bg.png"
            :style="'width:' + dataWidth + 'px;height:' + dataHeight + 'px;'"
          />
          <canvas id="puzzle-box" ref="puzzleBox" :width="dataWidth" :height="dataHeight"></canvas>
        </div>
        <div
          class="puzzle-lost-box"
          :style="'left:' + left_Num + 'px;width:' + dataWidth + 'px;height:' + dataHeight + 'px;'"
        >
          <canvas id="puzzle-shadow" ref="puzzleShadow" :width="dataWidth" :height="dataHeight"></canvas>
          <canvas id="puzzle-lost" ref="puzzleLost" :width="dataWidth" :height="dataHeight"></canvas>
        </div>
        <p
          class="ver-tips"
          ref="verTips"
          :style="'height: '+22*scale+'px;line-height:'+22*scale+'px;bottom: ' + (displayTips==true ? 0 : -22*scale ) +'px;font-size: '+12*scale+'px;'"
        >
          <template v-if="verification">
            <span :style="'color:#42ca6b;line-height:'+ 22*scale+'px;'">验证通过</span>
            <span :style="'margin-left:'+4*scale+'px;line-height:'+ 22*scale+'px;'">哇喔,怪物吃了拼图,快去登录吧~</span>
          </template>
          <template v-if="!verification">
            <span :style="'color:red;line-height:'+22*scale+'px;'">验证失败:</span>
            <span :style="'margin-left:'+4*scale+'px;line-height:'+ 22*scale+'px;'">拖动滑块将悬浮图像正确拼合</span>
          </template>
        </p>
      </div>
      <div
        class="re-btn"
        @click="refreshImg"
        :style="'height: '+28*scale+'px;padding: '+1*scale+'px '+16*scale+'px;font-size: '+18*scale+'px;'"
      >
        <!-- <a-icon type="redo" /> -->
      </div>
    </div>
    <br />
    <div class="slider-container" :style="'width:' + dataWidth + 'px;'">
      <div class="slider-bar" :style="'border-radius:'+ 24*scale+'px;'">
        <p
          class="slider-bar-text select-none"
          onselectstart="return false"
          :style="'line-height:' + 28*scale + 'px;font-size:'+12*scale+'px;'"
        >按住滑块， 拖动完成上方拼图</p>
      </div>
      <div
        class="slider-btn"
        ref="sliderBtn"
        @mousedown="startMove"
        @touchstart="startMove"
        :style="'top: '+ -1.80*scale + 'px;'"
      >
        <!-- <a-icon
          type="pause-circle"
          theme="twoTone"
          twoToneColor="#52c41a"
          :style="'font-size: '+44*scale+'px;'"
        /> -->
        <span class="slider-center-zzz">▍▍▍</span>
      </div>
    </div>
  </div>
</template>

<script lang="js">

export default {
  name: 'puzzle',
  data () {
    return {
      move: true,
      moveStart: '',
      displayTips: false,
      verification: false,
      randomX: null,
      randomY: null,
      imgRandom: '',
      left_Num: 0,
      dataWidth: 404,
      dataHeight: 90,
      puzzleSize: 100,
      deviation: 11,
      scale: 1,
      reTimer: null,
      isleavePage: false
    }
  },
  props: {
    width: {
      type: [String, Number],
      default: 340
    },
    height: {
      type: [String, Number],
      default: 120
    },
    cropImage: {
      type: [Boolean],
      default: false
    },
    puzzleImgList: {
      type: Array,
      default: () => [
        '../../../static/img/a.png'
      ]
    },
    PlSize: {
      type: [String, Number],
      default: 48
    },
    Deviation: {
      type: [String, Number],
      default: 4
    },
    padding: {
      type: [Number],
      default: 20
    }
  },
  methods: {
    /* 刷新 */
    RandomNum (Min, Max) {
      const Range = Max - Min + 1
      const Rand = Math.random()
      return Min + Math.floor(Rand * Range)
    },
    refreshImg () {
      const imgRandomNum = this.RandomNum(0, this.puzzleImgList.length - 1)
      this.imgRandom = this.puzzleImgList[imgRandomNum]
      this.initCanvas()
    },
    /* 画布初始化 */
    initCanvas () {
      this.clearCanvas()

      // let self = this
      const w = this.dataWidth
      const h = this.dataHeight
      //   let PL_Size = 48
      // 2019-02-12开始PL_Size使用可传递参数
      const plSize = this.puzzleSize
      const plDing = 20
      const minX = plDing + plSize
      const maxX = w - plDing - plSize - plSize / 6
      const maxY = plDing
      const jinUa = h - plDing - plSize - plSize / 6
      this.randomX = this.RandomNum(minX, maxX)
      this.randomY = this.RandomNum(jinUa, maxY)
      const X = this.randomX
      const Y = this.randomY
      this.left_Num = -X + 10
      const d = plSize / 3

      const c = this.$refs.puzzleBox
      const plOj = this.$refs.puzzleLost
      const cS = this.$refs.puzzleShadow
      const ctx = c.getContext('2d')
      const ctxl = plOj.getContext('2d')
      const ctxs = cS.getContext('2d')
      ctx.globalCompositeOperation = 'xor'
      ctx.shadowBlur = 10
      ctx.shadowColor = '#fff'
      ctx.shadowOffsetX = 3
      ctx.shadowOffsetY = 3
      ctx.fillStyle = 'rgba(0,0,0,0.7)'
      ctx.beginPath()
      ctx.lineWidth = '1'
      ctx.strokeStyle = 'rgba(0,0,0,0)'
      ctx.moveTo(X, Y)
      ctx.lineTo(X + d, Y)
      ctx.bezierCurveTo(X + d, Y - d, X + 2 * d, Y - d, X + 2 * d, Y)
      ctx.lineTo(X + 3 * d, Y)
      ctx.lineTo(X + 3 * d, Y + d)
      ctx.bezierCurveTo(X + 2 * d, Y + d, X + 2 * d, Y + 2 * d, X + 3 * d, Y + 2 * d)
      ctx.lineTo(X + 3 * d, Y + 3 * d)
      ctx.lineTo(X, Y + 3 * d)
      ctx.closePath()
      ctx.stroke()
      ctx.fill()

      const img = new Image()
      img.src = this.imgRandom
      img.onload = function () {
        ctxl.beginPath()
        ctxl.strokeStyle = 'rgba(0,0,0,0)'
        ctxl.moveTo(X, Y)
        ctxl.lineTo(X + d, Y)
        ctxl.bezierCurveTo(X + d, Y - d, X + 2 * d, Y - d, X + 2 * d, Y)
        ctxl.lineTo(X + 3 * d, Y)
        ctxl.lineTo(X + 3 * d, Y + d)
        ctxl.bezierCurveTo(X + 2 * d, Y + d, X + 2 * d, Y + 2 * d, X + 3 * d, Y + 2 * d)
        ctxl.lineTo(X + 3 * d, Y + 3 * d)
        ctxl.lineTo(X, Y + 3 * d)
        ctxl.closePath()
        ctxl.stroke()
        ctxl.shadowBlur = 10
        ctxl.shadowColor = 'black'
        ctxl.clip()
        ctxl.drawImage(img, 0, 0, w, h)
      }
      ctxs.beginPath()
      ctxs.lineWidth = '1'
      ctxs.strokeStyle = 'rgba(0,0,0,0)'
      ctxs.moveTo(X, Y)
      ctxs.lineTo(X + d, Y)
      ctxs.bezierCurveTo(X + d, Y - d, X + 2 * d, Y - d, X + 2 * d, Y)
      ctxs.lineTo(X + 3 * d, Y)
      ctxs.lineTo(X + 3 * d, Y + d)
      ctxs.bezierCurveTo(X + 2 * d, Y + d, X + 2 * d, Y + 2 * d, X + 3 * d, Y + 2 * d)
      ctxs.lineTo(X + 3 * d, Y + 3 * d)
      ctxs.lineTo(X, Y + 3 * d)
      ctxs.closePath()
      ctxs.stroke()
      ctxs.shadowBlur = 20
      ctxs.shadowColor = 'black'
      ctxs.fill()
    },
    /* 通过重置画布尺寸清空画布，这种方式更彻底 */
    clearCanvas () {
      const c = this.$refs.puzzleBox
      const a = this.$refs.puzzleLost
      const s = this.$refs.puzzleShadow
      c.setAttribute('height', c.getAttribute('height'))
      a.setAttribute('height', c.getAttribute('height'))
      s.setAttribute('height', c.getAttribute('height'))
    },
    /* 按住滑块后初始化移动监听，记录初始位置 */
    startMove (e) {
      if (this.verification) return
      this.move = !this.move
      e = e || window.event
      this.$refs.sliderBtn.style.backgroundPosition = '0 -216px'
      this.moveStart = e.pageX || e.targetTouches[0].pageX
      this.addMouseMoveListener()
    },
    /* 滑块移动 */
    moving (e) {
      const self = this
      e = e || window.event
      const moveX = e.pageX || e.targetTouches[0].pageX
      const d = moveX - self.moveStart
      const w = self.dataWidth
      const s = this.puzzleSize
      const padding = 20
      if (self.moveStart === '') {
        return ''
      }
      if (d < 0 || d > (w - padding - s)) {
        return ''
      }
      self.$refs.sliderBtn.style.left = d + 'px'
      self.$refs.sliderBtn.style.transition = 'inherit'
      self.$refs.puzzleLost.style.left = d + 'px'
      self.$refs.puzzleLost.style.transition = 'inherit'
      self.$refs.puzzleShadow.style.left = d + 'px'
      self.$refs.puzzleShadow.style.transition = 'inherit'
    },
    /* 移动结束，验证并回调 */
    moveEnd (e) {
      const self = this
      e = e || window.event
      const x = (e.pageX || e.changedTouches[0].pageX) - self.moveStart
      const v = self.randomX - 10
      const deviation = this.deviation
      const m = v - deviation
      const M = v + deviation
      if (self.moveStart !== '') {
        if (M > x && x > m) {
          self.displayTips = true
          self.verification = true
          setTimeout(function () {
            if (self.isleavePage) {
              return
            }
            self.displayTips = false
            self.initCanvas()
          }, 2000)
          /* 成功的回调函数 */
          //   self.onSuccess()
          this.$emit('onSuccess')
          setTimeout(() => {
            this.move = !this.move
          }, 1000)
        } else {
          self.displayTips = true
          self.verification = false
          setTimeout(function () {
            if (self.isleavePage) {
              return
            }
            self.displayTips = false
          }, 2000)
          /* 失败的回调函数 */
          this.$emit('onError')
          setTimeout(() => {
            this.move = !this.move
          }, 1000)
        }
      }
      if (typeof (self.$refs.sliderBtn) !== 'undefined' && typeof (self.$refs.puzzleLost) !== 'undefined' && typeof (self.$refs.puzzleShadow) !== 'undefined') {
        self.reTimer = setTimeout(function () {
          if (self.isleavePage) {
            return
          }
          if (self.verification) return
          self.$refs.sliderBtn.style.left = 0
          self.$refs.sliderBtn.style.transition = 'left 0.5s'
          self.$refs.puzzleLost.style.left = 0
          self.$refs.puzzleLost.style.transition = 'left 0.5s'
          self.$refs.puzzleShadow.style.left = 0
          self.$refs.puzzleShadow.style.transition = 'left 0.5s'
        }, 1000)
        self.$refs.sliderBtn.style.backgroundPosition = '0 -84px'
      }
      self.moveStart = ''
    },
    /* 绑定滑块移动与滑动结束，移动过程中鼠标可在绑定的区域 */
    addMouseMoveListener () {
      const self = this
      document.addEventListener('mousemove', self.moving)
      document.addEventListener('touchmove', self.moving)
      document.addEventListener('mouseup', self.moveEnd)
      document.addEventListener('touchend', self.moveEnd)
    }

  },
  mounted () {
    const self = this
    this.dataWidth = this.$refs.puzzle.clientWidth * 0.884
    this.dataHeight = this.dataWidth * (this.height / this.width)
    this.scale = this.$refs.puzzle.clientWidth / 260
    this.puzzleSize = this.dataWidth * (this.PlSize / this.width)
    setTimeout(self.refreshImg, 0)
  },
  created () {
    const imgRandomNum = this.RandomNum(0, this.puzzleImgList.length - 1)
    this.imgRandom = this.puzzleImgList[imgRandomNum]
    this.puzzleSize = Number(this.PlSize)
    this.deviation = Number(this.Deviation)

    // this.dataWidth = this.width
    // this.dataHeight = this.height
    // this.scale = this.width/260
  },
  watch: {
  },
  beforeDestroy () {
    this.isleavePage = true
  }
}
</script>

<style scoped>
.slider-btn {
    position: absolute;
    width: 52px;
    height: 49px;
    /* top: 17px; */
    /* left: 0; */
    top: -4px;
    z-index: 12;
    cursor: pointer;
    background-position: 0 -84px;
    transition: inherit;
    background-color: #f4f7fe;
    border-radius: 45%;
    display: flex;
}
.slider-center-zzz {
  text-align: center;
  line-height: 55px;
  position: absolute;
  color: #53C300;
  height: 40px;
  width: 100%;
}
.ver-tips {
  position: absolute;
  left: 0;
  bottom: -22px;
  background: rgba(255, 255, 255, 0.9);
  height: 22px;
  line-height: 22px;
  font-size: 12px;
  width: 100%;
  margin: 0;
  text-align: left;
  padding: 0 8px;
  transition: all 0.4s;
}
.slider-tips {
  bottom: 0;
}
.ver-tips i {
  display: inline-block;
  width: 22px;
  height: 22px;
  vertical-align: top;
  background-position: -4px -1229px;
}
.ver-tips span {
  display: inline-block;
  vertical-align: top;
  line-height: 22px;
  color: #455;
}
.active-tips {
  display: block;
}
.hidden {
  display: none;
}
.re-btn {
  position: absolute;
  left: 0;
  bottom: 0;
  height: 28px;
  padding: 0 16px;
}
.re-btn a {
  display: inline-block;
  width: 14px;
  height: 14px;
  margin: 0px 0;
  background-position: 0 -1179px;
  cursor: pointer;
}
.re-btn a:hover {
  background-position: 0 -1193px;
}

.puzzle-container {
  position: relative;
  /* padding: 16px 16px 28px; */
  /* border: 1px solid #ddd; */
  background: #e0e1e3;
  /* border-radius: 16px; */

  /* height: 83px;  */
  /* width: 340px; */
}
.slider-container {
  width: 274px !important;
  position: relative;
  margin: auto;
  margin-left: 13px;
}
.slider-bar {
  border: 1px solid #c3c3c3;
  border-radius: 24px;
  background: #e0e1e3;
  box-shadow: 0 1px 1px rgba(12, 10, 10, 0.2) inset;
}
.slider-bar-text {
  font-size: 14px !important;
  color: #486c80;
  line-height: 28px;
  margin: 0;
  text-align: right;
  padding-right: 22px;
}

#puzzle-box {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 22;
}
#puzzle-shadow {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 22;
}
#puzzle-lost {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 33;
}
.puzzle-lost-box {
  position: absolute;
  width: 260px;
  height: 116px;
  left: 0;
  top: 0;
  z-index: 111;
}
</style>
